package com.bestom.waterquality.logic;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Spinner;

import com.bestom.waterquality.R;
import com.bestom.waterquality.entity.Constant;
import com.bestom.waterquality.util.SharedPreferencesUtil;
import com.bestom.waterquality.util.ToastUtil;
import com.suke.widget.SwitchButton;

/**
 * 图像设置
 */
public class ImageSet {

    private Context mContext;

    private ImageSet() {
    }

    private static ImageSet _instance = null;

    public static ImageSet getInstance() {
        if (_instance == null)
            _instance = new ImageSet();
        return _instance;
    }

    public void setDataAndListen(Context context, View view) {

        if (mContext == null){
            mContext = context;

            String value = (String) SharedPreferencesUtil.getInstance().getValue(Constant.imageSet, "false-0-0-0");
            Log.d("ImageSet","value--->" + value);
            String[] strings = value.split("-");
            SwitchButton idDisplay = view.findViewById(R.id.id_display);
            if (strings[0].equals("false")) idDisplay.setChecked(false);
            else idDisplay.setChecked(true);
            Spinner textSize = view.findViewById(R.id.text_size);
            textSize.setSelection(Integer.parseInt(strings[1]));
            Spinner textColor = view.findViewById(R.id.text_color);
            textColor.setSelection(Integer.parseInt(strings[2]));
            Spinner watermarkPosition = view.findViewById(R.id.watermark_position);
            watermarkPosition.setSelection(Integer.parseInt(strings[3]));
            view.findViewById(R.id.image_set_save).setOnClickListener(view1 -> {
                SharedPreferencesUtil.getInstance().putValue(Constant.imageSet,
                        idDisplay.isChecked() + "-" +
                        textSize.getSelectedItemPosition() + "-" +
                        textColor.getSelectedItemPosition() + "-" +
                        watermarkPosition.getSelectedItemPosition());
                ToastUtil.toast("设置成功");
            });
        }
    }
}
